package com.fahai.web.controller;

import com.fahai.common.PageVo;
import com.fahai.utils.json.JsonResult;
import com.fahai.web.entity.AllConfig;
import com.fahai.web.service.AllConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * Created by Lilinjun on 17/5/22.
 */

@Controller
@EnableGlobalMethodSecurity(prePostEnabled = true)
@RequestMapping(value = "/operate")
public class OperatorController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(OperatorController.class);

    @Autowired
    private AllConfigService allConfigService;


    /**
     * 跳转到查询页面
     *
     * @param
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','运营规则设定-0','运营规则设定-1')")
    @RequestMapping(value = "/operating_rules", method = RequestMethod.GET)
    public String allconfigToSelect(Model model) {
        model.addAttribute("selectTab", "运营规则设定");

        return "operating_rules";
    }

    /**
     * 运营规则分页条件查询
     *
     * @param pageVo
     * @return
     * @throws IOException
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','运营规则设定-0','运营规则设定-1')")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public JsonResult list(PageVo pageVo, AllConfig allConfig) throws IOException {
        pageVo = allConfigService.getAllConfigList(allConfig, pageVo);
        return ok("查询", pageVo);
    }


    /**
     * 编辑运营规则信息
     *
     * @param model
     * @return
     */


    @PreAuthorize("hasAnyAuthority('ADMIN','运营规则设定-0')")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult toupdateConfig(AllConfig allConfig, Model model) {
        if(allConfig.getConfEname().contains("price")){
            allConfig.setConfValue(String.valueOf(  ( new Double(Double.parseDouble(allConfig.getConfValue())*100 )).intValue() ));
        }

        allConfigService.updateAllConfig(allConfig);
        return ok("修改成功");
    }


    /**
     * 停用运营规则
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','运营规则设定-0')")
    @RequestMapping(value = "/stop", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult tostopConfig(@RequestParam Integer allId) {
        AllConfig allConfig = new AllConfig();
        allConfig.setAllId(allId);
        allConfigService.stopConfig(allConfig);
        return ok("修改成功");
    }


    /**
     * 启用运营规则
     *
     * @return
     */

    @PreAuthorize("hasAnyAuthority('ADMIN','运营规则设定-0')")
    @RequestMapping(value = "/active", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult toActiveConfig(@RequestParam Integer allId) {
        AllConfig allConfig = new AllConfig();
        allConfig.setAllId(allId);
        allConfigService.activeConfig(allConfig);
        return ok("修改成功");
    }


    /**
     * 增加运营规则信息
     * 参数类型（conf_type=20)的全局变量含有2个属性值， 保存的时候用"#"号进行拼接存放到 conf_value字段中；
     * 其他类型的属性值按照正常的方式入库。
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','运营规则设定-0')")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addConfig(Model model, AllConfig allConfig) {


        //是否存在同英文标识的参数
        boolean ishave = checkIsExist(allConfig.getConfEname());
        if (ishave) {
            return fail("已经存在改类型的英文标识");
        } else {
            if(allConfig.getConfEname().contains("price")){
                allConfig.setConfValue(String.valueOf(  ( new Double(Double.parseDouble(allConfig.getConfValue())*100 )).intValue() ));


            }
            switch (allConfig.getConfType()) {
                case 20:
                    String bak = allConfig.getBak();
                    allConfig.setConfValue(allConfig.getConfValue() + "#" + bak);
                    allConfig.setBak(null);
                default:
                    allConfig.setBak(null);
            }

            allConfig.setIsStatus(0);//TODO 默认是0 ,以后根据需要
            allConfig.setConfEname(allConfig.getConfEname());
            allConfigService.insertAllConfig(allConfig);
            return ok("修改成功");
        }

    }


    /**
     * 判断是否已经存在英文标识
     *
     * @param confEname
     * @return
     */
    private boolean checkIsExist(String confEname) {
        if (null == confEname) {
            return false;
        } else {
            AllConfig allConfig = new AllConfig();
            allConfig.setConfEname(confEname);
            PageVo pageVo = allConfigService.getAllConfigList(allConfig, new PageVo());
            if (pageVo.getResult() == null||pageVo.getResult().size()==0) {
                return false;
            } else {
                return true;
            }
        }
    }
}
